<template>
  <div class="shopcart">
    <div class="top-nav">
      <div class="title">购物车</div>
      <div class="edit" @click="toEdit">
        {{ editStatus == false ? "编辑" : "取消" }}
      </div>
    </div>
    <div class="content">
      <!-- 未登录提示 -->
      <div class="top-tip" v-if="token==null">
        <div class="tip">登录后享受更多优惠</div>
        <div class="go-to-login">去登录 <van-icon name="arrow" /></div>
      </div>
      <!-- 购物车为空提示 -->
      <div class="empty" v-if="shopcartData.length == 0">
        <van-empty
          class="custom-image"
          :image="emptyImg"
          description="购物车还是空的"
        />
        <div class="go-to-home">随便看看</div>
      </div>
      <!-- 购物车列表 -->
      <div class="goods-wrap" v-if="shopcartData.length > 0">
        <div class="goods-top-nav">
          <div class="left">
            <van-checkbox
              v-model="Allchecked"
              checked-color="#ee0a24"
              @click="ChooseAll"
            ></van-checkbox>
            <div class="text">小米自营</div>
          </div>
          <div class="right"><van-icon name="info-o" /> 已免运费</div>
        </div>
        <div
          class="content-list"
          v-for="(item, index) in shopcartData"
          :key="item.createTime"
        >
          <van-swipe-cell>
            <van-card :price="item.price" :title="item.name" class="goods-card">
              <!-- 商品规格 -->
              <template #desc>
                <div class="goods-desc">
                  {{ item.version + " " + item.color }}
                  <van-icon name="arrow-down" />
                </div>
              </template>
              <!-- 商品图片 -->
              <template #thumb>
                <div class="left">
                  <van-checkbox
                    v-model="item.checkState"
                    checked-color="#ee0a24"
                    @click="chooseOne(item.checkState)"
                  ></van-checkbox>
                  <div class="imgbox" @click="goToGoodsDetail({id:15694,router:$router})">
                    <img class="auto-img" :src="item.img.img" alt="" />
                  </div>
                </div>
              </template>
              <!-- 商品数量 -->
              <template #num>
                <div class="num-box">
                  <div
                    class="reduce"
                    :style="item.num > 1 ? 'color:#000' : ''"
                    @click="reduceNum(item)"
                  >
                    <van-icon name="minus" />
                  </div>
                  <div class="num">{{ item.num }}</div>
                  <div
                    class="add"
                    :style="item.num >= 1 ? 'color:#000' : ''"
                    @click="addNum(item)"
                  >
                    <van-icon name="plus" />
                  </div>
                </div>
              </template>
            </van-card>
            <!-- 删除 -->
            <template #right>
              <van-button
                square
                text="删除"
                type="danger"
                class="delete-button"
                @click="deleteGoods(item)"
              />
            </template>
          </van-swipe-cell>
        </div>
      </div>
      <!-- 猜你喜欢 -->
      <div class="bottom-area">
        <div class="title">
          <van-icon name="minus" /> 猜你喜欢 <van-icon name="minus" />
        </div>
        <div class="goods-list-wrap">
          <div class="goods-list" v-for="item in 10" :key="item" @click="goToGoodsDetail({id:15694,router:$router})">
            <div class="img-box"></div>
            <div class="name">Redmi K40S</div>
            <div class="desc">口碑真旗舰</div>
            <div class="tag-wrap">
              <div class="tag-list">加价购</div>
            </div>
            <div class="price">
              <div class="new-price">
                ¥<span class="goods-price">1799</span>起
              </div>
              <div class="old-price">¥2099</div>
            </div>
            <div class="look-for">看相似</div>
          </div>
        </div>
      </div>
      <!-- 底部提交订单 -->
      <van-submit-bar
        :price="totalPrice * 100"
        button-text="提交订单"
        @submit="onSubmit"
        v-show="!editStatus"
      >
        <van-checkbox
          checked-color="#ee0a24"
          v-model="Allchecked"
          @click="ChooseAll"
          >全选</van-checkbox
        >
      </van-submit-bar>
      <van-submit-bar
        button-text="删除所选"
        @submit="onSubmit"
        v-show="editStatus"
      >
        <van-checkbox
          checked-color="#ee0a24"
          v-model="Allchecked"
          @click="ChooseAll"
          >全选</van-checkbox
        >
      </van-submit-bar>
    </div>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
export default {
  name: "ShopcartView",
  data() {
    return {
      //购物车为空时显示该图片
      emptyImg: require("@/assets/images/Snipaste_2022-12-08_16-40-58.jpg"),
      //购物车数据
      shopcartData: [],
      //全选
      Allchecked: false,
      //单选个数
      chooseOneCount: 0,
      //登录状态
      token: null,
      //编辑状态
      editStatus: false,
    };
  },
  computed: {
    totalPrice() {
      let total = 0;
      this.shopcartData.forEach((item) => {
        if (item.checkState) {
          total += Number(item.price) * Number(item.num);
        }
      });
      return total;
    },
  },
  activated() {
    //获取购物车数据
    this.shopcartData = JSON.parse(localStorage.getItem("shopcartData"));
    //获取登录状态
    this.token = localStorage.getItem("token");
    this.ChooseAll();
    if (this.shopcartData.length==0) {
      this.Allchecked=false
    }
  },
  methods: {
    ...mapMutations(['addDataToConfirmView','goToGoodsDetail']),
    //提交与编辑删除
    onSubmit() {
      if (this.token == null && !this.editStatus) {
        this.$router.push({ name: "loginView" });
      } else if (this.token != null && !this.editStatus) {
        if (this.totalPrice==0) {
          this.$toast('亲,您还没有选择任何商品呢')
        }else{
          let data = this.shopcartData.filter(item => {
            return item.checkState==true
          })
          this.addDataToConfirmView({data,totalPrice:this.totalPrice})
          this.$router.push({ name: "CofirmBuyView" });
        }
      } else {
        if (this.editStatus) {
          let arr = []
          this.shopcartData.forEach((item, index) => {
            if (!item.checkState) {
              arr.push(item)
              if (item.checkState) {
                this.chooseOneCount--;
              }
              if (this.chooseOneCount == this.shopcartData.length) {
                this.Allchecked = true;
              } else {
                this.Allchecked = false;
              }
            }
          });
          this.shopcartData=arr
          localStorage.setItem("shopcartData", JSON.stringify(this.shopcartData));
        }
        this.chooseOneCount=0
        if (this.shopcartData.length==0) {
          this.Allchecked=false
        }
      }
    },
    //增加按钮
    addNum(item) {
      item.num++;
    },
    //减少按钮
    reduceNum(item) {
      if (item.num == 1) {
        this.$toast("最少数量为1");
        return;
      }
      item.num--;
    },
    //全选
    ChooseAll() {
      if (this.Allchecked) {
        this.shopcartData.forEach((item) => {
          item.checkState = true;
        });
        this.chooseOneCount = this.shopcartData.length;
      } else if (!this.Allchecked) {
        this.shopcartData.forEach((item) => {
          item.checkState = false;
        });
        this.chooseOneCount = 0;
      }
    },
    //单选
    chooseOne(checked) {
      if (checked) {
        this.chooseOneCount++;
        if (this.chooseOneCount == this.shopcartData.length) {
          this.Allchecked = true;
        } else {
          this.Allchecked = false;
        }
      } else if (!checked) {
        this.chooseOneCount--;
        if (this.chooseOneCount == this.shopcartData.length) {
          this.Allchecked = true;
        } else {
          this.Allchecked = false;
        }
      }
    },
    //删除
    deleteGoods(item) {
      this.shopcartData.forEach((item2, index) => {
        if (item2.createTime == item.createTime) {
          this.shopcartData.splice(index, 1);
          if (item.checkState) {
            this.chooseOneCount--;
          }
          if (this.chooseOneCount == this.shopcartData.length) {
            this.Allchecked = true;
          } else {
            this.Allchecked = false;
          }
        }
      });
      localStorage.setItem("shopcartData", JSON.stringify(this.shopcartData));
      if (this.shopcartData.length==0) {
        this.Allchecked=false
      }
    },
    //编辑
    toEdit() {
      this.editStatus = !this.editStatus;
      if (this.editStatus) {
        this.shopcartData.forEach((item) => {
          item.checkState = false;
        });
        this.Allchecked = false;
        this.chooseOneCount = 0;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.shopcart {
  padding: 0 10px 60px;
  box-sizing: border-box;
  .top-nav {
    width: 100%;
    height: 50px;
    text-align: center;
    line-height: 50px;
    position: fixed;
    background-color: #fff;
    z-index: 10;
    left: 0;
    .goback {
      font-weight: 600;
      font-size: 20px;
      position: absolute;
      left: 15px;
      top: 0px;
    }
    .title {
      font-weight: 600;
      font-size: 20px;
    }
    .edit {
      font-weight: 600;
      font-size: 16px;
      position: absolute;
      right: 20px;
      top: 0px;
    }
  }
  .content {
    width: 100%;
    padding: 60px 10px 0;
    box-sizing: border-box;
    background-color: rgb(244, 244, 244);
    .top-tip {
      width: 100%;
      background-color: #fff;
      border-radius: 8px;
      height: 45px;
      line-height: 45px;
      display: flex;
      justify-content: space-between;
      padding: 0 10px;
      box-sizing: border-box;
      .tip {
        font-weight: 600;
        font-size: 14px;
      }
      .go-to-login {
        color: #666;
      }
    }
    .empty {
      ::v-deep .custom-image .van-empty__image {
        width: 150px;
        height: 120px;
        img {
          display: block;
          width: 100%;
          position: relative;
          left: 10px;
        }
      }
      .go-to-home {
        width: 60px;
        height: 25px;
        border: 1px solid #ddd;
        line-height: 25px;
        text-align: center;
        margin: 0 auto;
        border-radius: 999px;
        color: #777;
        padding: 2px 12px;
      }
    }
    .goods-wrap {
      width: 100%;
      background-color: #fff;
      border-radius: 10px;
      margin-top: 20px;
      overflow: hidden;
      .goods-top-nav {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 10px;
        box-sizing: border-box;
        .left {
          display: flex;
          align-items: center;
          .text {
            margin-left: 10px;
            font-weight: 600;
          }
        }
      }
      .content-list {
        width: 100%;
        .goods-card {
          margin: 0;
          background-color: #fff;
          text-align: left;
        }
        .left {
          width: 110px;
          display: flex;
          align-items: center;
          margin-right: 10px;
          .imgbox {
            width: 88px;
            height: 88px;
            flex: 1;
          }
        }
        ::v-deep .van-card__content {
          margin-left: 15px;
          .van-card__title {
            font-weight: 600;
            font-size: 16px;
          }
          .goods-desc {
            background-color: rgb(243, 243, 243);
            border-radius: 4px;
            padding: 5px;
            margin-top: 10px;
          }
          .van-card__price {
            color: rgb(252, 119, 30);
            font-weight: 600;
            .van-card__price-integer {
              font-size: 18px;
            }
          }
        }
        .delete-button {
          height: 100%;
        }
        .num-box {
          display: flex;
          border: 1px solid #ddd;
          border-radius: 999px;
          .reduce {
            padding: 0 5px;
            &.reduceActive {
              color: #000 !important;
            }
          }
          .add {
            padding: 0 5px;
            &.addActive {
              color: #000;
            }
          }
          .num {
            padding: 0 10px;
            border-left: 1px solid #ddd;
            border-right: 1px solid #ddd;
            color: #000;
            font-weight: 600;
          }
        }
      }
    }
    .bottom-area {
      margin-top: 50px;
      padding-bottom: 40px;
      .title {
        font-weight: 600;
        font-size: 16px;
      }
      .goods-list-wrap {
        width: 100%;
        margin-top: 20px;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        .goods-list {
          width: calc(50% - 5px);
          height: 250px;
          background-color: #fff;
          border-radius: 8px;
          margin-right: 10px;
          margin-bottom: 10px;
          padding: 10px;
          box-sizing: border-box;
          text-align: left;
          position: relative;
          &:nth-child(2n) {
            margin-right: 0;
          }
          .img-box {
            height: 130px;
            background: url('@/assets/images/home/微信图片_20221208153637.jpg') no-repeat center center;
            background-size: cover;
          }
          .name {
            margin: 10px 0;
            font-weight: 600;
            font-size: 14px;
          }
          .desc {
            margin-bottom: 10px;
          }
          .tag-wrap {
            margin-bottom: 15px;
            .tag-list {
              width: 40px;
              height: 16px;
              line-height: 16px;
              padding: 0 5px;
              border-radius: 5px;
              text-align: center;
              border: 1px solid rgb(252, 119, 30);
              color: rgb(252, 119, 30);
            }
          }
          .price {
            font-weight: 600;
            font-size: 12px;
            display: flex;
            .goods-price {
              font-size: 18px;
            }
            .old-price {
              position: relative;
              top: 6px;
              left: 5px;
              text-decoration: line-through;
              color: #aaa;
            }
          }
          .look-for {
            position: absolute;
            right: 0;
            bottom: 10px;
            width: 50px;
            height: 20px;
            border-top-left-radius: 999px;
            border-bottom-left-radius: 999px;
            background-color: #eee;
            color: #666;
            text-align: center;
            line-height: 20px;
          }
        }
      }
    }
  }
  ::v-deep .van-submit-bar {
    bottom: 48px;
    .van-checkbox {
      position: absolute;
      left: 20px;
    }
  }
}
</style>